#include "myimageprovider.h"

#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>
#include <qfile.h>
MyImageProvider::MyImageProvider(ImageType type, Flags flags) :
    QQuickImageProvider(type,flags)
{
    manager = new QNetworkAccessManager;
    QString val;
    QFile file;
    file.setFileName("D:/Project/robot/club/qmlPostTest/project/test.txt");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    val = file.readAll();
    file.close();
    myGetUrlImage.getImageUrlList(val);
    myGetUrlImage.startgetPictureList();
}

MyImageProvider::~MyImageProvider()
{
    delete manager;
}
QPixmap MyImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
    QPixmap map;
    if(id == "abcd")
    {
        if(myGetUrlImage.TagStrImageList.count() > 0)
        {
            return myGetUrlImage.TagStrImageList.at(0).picture;
        }
    }
    else if(id == "abcs")
    {
        if(myGetUrlImage.TagStrImageList.count() > 1)
        {
            return myGetUrlImage.TagStrImageList.at(1).picture;
        }
    }
    return map;
}
//------------------------------------QEventLoop 的应用--------------------------
//QImage MyImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
//{
//    qDebug() << "id: " << id;
//    qDebug() << "reequestedSize: " << requestedSize.width() + " " + requestedSize.height();
//    QUrl url("http://lorempixel.com/" + id);
//    QNetworkReply* reply = manager->get(QNetworkRequest(url));
//    QEventLoop eventLoop;
//    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
//    eventLoop.exec();
//    if (reply->error() != QNetworkReply::NoError)
//        return QImage();
//    QImage image = QImage::fromData(reply->readAll());
//    size->setWidth(image.width());
//    size->setHeight(image.height());
//    return image;
//}
